<?php
$event_id = $_GET['event_id'];

if ($_GET['delete']) {
	$registration_id = $_GET['registration_id'];
	$wpdb->query("DELETE FROM $em_registration_table where id = '$registration_id'");
	$wpdb->query("DELETE FROM $em_answer_table where registration_id = '$registration_id'");
}
?>
<div class="wrap">
<h2>Participant List</h2>
<h3><a href="<?php echo EM_URL ?>event-list.php">&laquo; Event List</a> - <a href="<?php echo EM_URL ?>add-participant.php&amp;event_id=<?php echo $event_id ?>">Add Participant &raquo;</a>
</h3>
<p><strong>Download:</strong> <a href="<?php echo EM_BASE_URL ?>extract-participants.php?action=simple&amp;event_id=<?php echo $event_id ?>" target="_blank">participant summary</a>, <a href="<?php echo EM_BASE_URL ?>extract-participants.php?action=complete&amp;event_id=<?php echo $event_id ?>" target="_blank">all registration data</a>.</p>
<table id="the-list-x" width="100%" cellpadding="3" cellspacing="3">
	<tr>
		<th>Confirmation</th>
		<th>First Name</th>
		<th>Last Name</th>
		<th>City</th>
		<th>State</th>
		<th></th>
		<th></th>
	</tr>
	<?php
	$sql = "SELECT * from $em_registration_table";
	$where_clause = " where 1 = 1";
	$offset = $_GET['paged'] * 10;
	$where_clause .= " and event_id = '$event_id'";
	$sql .= $where_clause . " limit $offset, 10";
	$participants = $wpdb->get_results($sql);

	if ($participants)
	foreach ($participants as $participant) {
	$class = ('alternate' == $class) ? '' : 'alternate';
	?>
	<tr class="<?php echo $class; ?>">
		<td><?php echo $participant->event_id ?>-<?php echo $participant->id ?></td>
		<td><?php echo $participant->first_name ?></td>
		<td><?php echo $participant->last_name ?></td>
		<td><?php echo $participant->city ?></td>
		<td><?php echo $participant->state ?></td>
		<td><a href="<?php echo EM_URL ?>edit-participant.php&amp;registration_id=<?php echo $participant->id ?>" class="edit">edit</a></td>
		<td><a href="<?php echo EM_URL ?>participant-list.php&amp;event_id=<?php echo $event_id ?>&amp;delete=true&amp;registration_id=<?php echo $participant->id ?>" class="delete" onclick="return confirm('Are you sure you want to delete this participant?')">delete</a></td>
	</tr>
	<?php } if (!$participants) echo "<td colspan='6'><strong>No one has signed up yet.</strong></td>";	?>
</table>

<?php
	$p_count = $wpdb->get_var("select count(*) from $em_registration_table" . $where_clause);
	$page_count = $p_count / 10;
	$paged = $_GET['paged'] + 1;
	$page_url = EM_URL . 'participant-list.php&event_id=' . $event_id . '&paged=';
?>

<div class="navigation">
	<div class="alignleft">
	<?php if ($page_count > $paged) :
		//$paged += 1; ?>
		<a href="<?php echo $page_url . $paged ?>"><?php _e('&laquo; Previous Entries') ?></a>
	<?php endif; ?>
	</div>
	<div class="alignright">
	<?php if (1 < $paged) :
		$paged -= 2; ?>
		<a href="<?php echo $page_url . $paged ?>"><?php _e('Next Entries &raquo;') ?></a>
	<?php endif; ?>
	</div>
</div>
</div>